package com.guyaning.deyson_ec.ec.main.sort.list;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.ui.recycler.ItemType;
import com.guyaning.deyson_core.core.ui.recycler.MultipleFields;
import com.guyaning.deyson_core.core.ui.recycler.MultipleItemBean;
import com.guyaning.deyson_core.core.ui.recycler.MultipleRecycleAdapter;
import com.guyaning.deyson_core.core.ui.recycler.MultipleViewHolder;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.main.sort.SortDelegate;
import com.guyaning.deyson_ec.ec.main.sort.content.ContentDelegate;

import java.util.List;

/**
 * Created by Administrator on 2018/6/27.
 */

public class SortRecyclerAdapter extends MultipleRecycleAdapter {

    private SortDelegate DELEGATE;

    private int mPrePosition = 0;

    public SortRecyclerAdapter(List<MultipleItemBean> data, SortDelegate delegate) {
        super(data);

        this.DELEGATE = delegate;

        //添加垂直菜单布局
        addItemType(ItemType.VERTICAL_MENU_LIST, R.layout.item_vertical_menu_list);
    }

    @Override
    protected void convert(final MultipleViewHolder helper, MultipleItemBean item) {
        super.convert(helper, item);

        switch (helper.getItemViewType()) {

            case ItemType.VERTICAL_MENU_LIST:

                String text = item.getFiled(MultipleFields.TEXT);

                boolean isClick = item.getFiled(MultipleFields.TAG);

                AppCompatTextView name = helper.getView(R.id.tv_vertical_item_name);

                View line = helper.getView(R.id.view_line);

                View itemView = helper.itemView;

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int currentPosition = helper.getAdapterPosition();

                        if(mPrePosition!=currentPosition){

                            //先还原上一个
                            getData().get(mPrePosition).setField(MultipleFields.TAG,false);
                            notifyItemChanged(mPrePosition);

                            //设置当前的Item
                            getData().get(currentPosition).setField(MultipleFields.TAG,true);
                            notifyItemChanged(currentPosition);

                            //将当前的位置传递给上一个
                            mPrePosition = currentPosition;

                            int contentId = getData().get(currentPosition).getFiled(MultipleFields.ID);

                            showContent(contentId);
                        }
                    }
                });

                if (!isClick) {
                    line.setVisibility(View.INVISIBLE);
                    name.setTextColor(ContextCompat.getColor(mContext, R.color.we_chat_black));
                    itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.item_background));
                } else {
                    line.setVisibility(View.VISIBLE);
                    name.setTextColor(ContextCompat.getColor(mContext, R.color.app_main));
                    line.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_main));
                    itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.item_background));
                }

                helper.setText(R.id.tv_vertical_item_name,text);


                break;
            default:
                break;
        }
    }

    private  void  showContent(int contentId){

        ContentDelegate delegate = ContentDelegate.newInstance(contentId);

        switchContent(delegate);
    }

    private void switchContent(ContentDelegate delegate) {

        final DeysonDelegate contentDelegate =  DELEGATE.findChildFragment(ContentDelegate.class);

        if(contentDelegate!=null){
            contentDelegate.replaceFragment(delegate,false);
        }
    }

}
